clear all
local bs = $bs

global lhs diff_tfpr

******************************************************************************
********************************** Welfare Regressions **************
******************************************************************************
use "$tempfold/pre_reg_data_ProdR_new_kl.dta", clear
gen q = ln(sales_quantity)
gen P = sales_value32 / sales_quantity
gen p = ln(P)
bys year: egen mean_q = mean(q)
bys year: egen mean_p = mean(p) 

gen sigma_hat = (1 / (1 - delta_hat))
gen eta_hat_ijt = (1 / (sigma_hat - 1)) * (q - mean_q) + (sigma_hat / (sigma_hat - 1)) * (p - mean_p)

gen Q_hat_ijt = (sales_quantity *exp(eta_hat_ijt) )^((sigma_hat - 1)/sigma_hat)
gen P_hat_ijt = (P/exp(eta_hat_ijt))^(1-sigma_hat)


	collapse (sum) sales_value Q_hat_ijt P_hat_ijt (mean) beta_* sigma_* delta_* l k ln_IND ln_IVS ///
		omega_* d_* h_* cleaned_number_prods *IPT_* ///
		, by(co_code1 year nic_08_2dig nic_08_4dig)
		
	merge 1:1 co_code1 year using "$tempfold/add_drop_categories_ProdR_new_kl.dta"
	keep if _merge == 3
	drop _merge	

cap rename full_dif_TFPR diff_tfpr

xtset  co_code1 year, yearly
destring(nic_08_2dig), g(nic2)

gen Q_hat_it = Q_hat_ijt ^ (sigma_hat / (sigma_hat - 1))
gen q_hat_it = ln(Q_hat_it)
gen P_hat_it = P_hat_ijt ^ (1 / (1 - sigma_hat))
gen p_hat_it = ln(P_hat_it)

gen diff_ln_IND = ln_IND - L.ln_IND
gen diff_ln_IVS = ln_IVS - L.ln_IVS
*gen diff_tfpr = tfpr - L.tfpr
gen diff_q_hat_it = q_hat_it - L.q_hat_it
gen diff_p_hat_it = p_hat_it - L.p_hat_it

foreach var in wIPT_IND_CHN wIPT_LMI_CHN_EXCL_IND rwIPT_IND_CHN rwIPT_LMI_CHN_EXCL_IND {
	generate diff_`var' = ln_`var' - L.ln_`var'
}
foreach v in $lhs diff_ln_IND diff_ln_IVS diff_q_hat_it diff_p_hat_it {
	drop if missing(`v')

}

global rhs1 diff_ln_IND 
global rhs2 diff_wIPT_IND_CHN
global ivs1 diff_ln_IVS 
global ivs2 diff_wIPT_LMI_CHN_EXCL_IND
global ctrl 


	ivreghdfe drop_term ($rhs1 $rhs2 = $ivs1 $ivs2 ) $ctrl  , a(year#nic2) first cluster(nic_08_4dig)
	generate sample0 = e(sample)
	keep if sample0 == 1

merge 1:m  co_code1 year using "$tempfold/bssample2"
keep if _merge==3
drop _merge

local n = 2
foreach var in $lhs diff_q_hat_it diff_p_hat_it {	
	
	preserve
		ivreghdfe `var' ($rhs1 $rhs2 = $ivs1 $ivs2 ) $ctrl , a(year) first cluster(nic_08_4dig)
				local b`n'_y = _b[diff_ln_IND]
				local se_b`n'_y = _se[diff_ln_IND]
				cap local I_b2_y = _b[diff_wIPT_IND_CHN]
				cap local se_I_b2_y = _se[diff_wIPT_IND_CHN]
				cap local I_b3_y = _b[diff_wIPT_LMI_CHN_EXCL_IND]
				cap local se_I_b3_y = _se[diff_wIPT_LMI_CHN_EXCL_IND]
				local N`n'_y = e(N)
				mat f = e(first)
				local F_b`n'_y = round(f[8,1], .01)
				cap local F_I_b`n'_y = round(f[8,2], .01)
				
		ivreghdfe `var' ($rhs1 $rhs2 = $ivs1 $ivs2 ) $ctrl , a(nic2 year) first cluster(nic_08_4dig)
				local b`n'_yn = _b[diff_ln_IND]
				local se_b`n'_yn = _se[diff_ln_IND]
				cap local I_b2_yn = _b[diff_wIPT_IND_CHN]
				cap local se_I_b2_yn = _se[diff_wIPT_IND_CHN]
				cap local I_b3_yn = _b[diff_wIPT_LMI_CHN_EXCL_IND]
				cap local se_I_b3_yn = _se[diff_wIPT_LMI_CHN_EXCL_IND]
				local N`n'_yn = e(N)
				mat f = e(first)
				local F_b`n'_yn = round(f[8,1], .01)
				cap local F_I_b`n'_yn = round(f[8,2], .01)		
				
		ivreghdfe `var' ($rhs1 $rhs2 = $ivs1 $ivs2 ) $ctrl , a(nic2#year) first cluster(nic_08_4dig)
				local b`n'_ycn = _b[diff_ln_IND]
				local se_b`n'_ycn = _se[diff_ln_IND]
				cap local I_b2_ycn = _b[diff_wIPT_IND_CHN]
				cap local se_I_b2_ycn = _se[diff_wIPT_IND_CHN]
				cap local I_b3_ycn = _b[diff_wIPT_LMI_CHN_EXCL_IND]
				cap local se_I_b3_ycn = _se[diff_wIPT_LMI_CHN_EXCL_IND]
				local N`n'_ycn = e(N)
				mat f = e(first)
				local F_b`n'_ycn = round(f[8,1], .01)
				cap local F_I_b`n'_ycn = round(f[8,2], .01)

		***
		clear
		set obs 1
		gen bs = `bs'

		gen `var'_b`n'_y = `b`n'_y'
		cap gen `var'_I_b2_y = `I_b2_y'
		cap gen `var'_I_b3_y = `I_b3_y'
		gen `var'_N`n'_y = `N`n'_y'
		gen `var'_F_b`n'_y = `F_b`n'_y'
		cap gen `var'_F_I_b`n'_y = `F_I_b`n'_y'

		gen `var'_b`n'_yn = `b`n'_yn'
		cap gen `var'_I_b2_yn = `I_b2_yn'
		cap gen `var'_I_b3_yn = `I_b3_yn'
		gen `var'_N`n'_yn = `N`n'_yn'
		gen `var'_F_b`n'_yn = `F_b`n'_yn'
		cap gen `var'_F_I_b`n'_yn = `F_I_b`n'_yn'

		gen `var'_b`n'_ycn = `b`n'_ycn'
		cap gen `var'_I_b2_ycn = `I_b2_ycn'
		cap gen `var'_I_b3_ycn = `I_b3_ycn'
		gen `var'_N`n'_ycn = `N`n'_ycn'
		gen `var'_F_b`n'_ycn = `F_b`n'_ycn'
		cap gen `var'_F_I_b`n'_ycn = `F_I_b`n'_ycn'
		save "$tempfold/bs2_welfare_`var'", replace
	
	restore
}

	



global rhs1 diff_ln_IND 
global rhs2 
global ivs1 diff_ln_IVS 
global ivs2 
global ctrl diff_wIPT_LMI_CHN_EXCL_IND
local n = 3
foreach var in $lhs diff_q_hat_it diff_p_hat_it {	
	
	preserve
		ivreghdfe `var' ($rhs1 $rhs2 = $ivs1 $ivs2 ) $ctrl , a(year) first cluster(nic_08_4dig)
				local b`n'_y = _b[diff_ln_IND]
				local se_b`n'_y = _se[diff_ln_IND]
				cap local I_b2_y = _b[diff_wIPT_IND_CHN]
				cap local se_I_b2_y = _se[diff_wIPT_IND_CHN]
				cap local I_b3_y = _b[diff_wIPT_LMI_CHN_EXCL_IND]
				cap local se_I_b3_y = _se[diff_wIPT_LMI_CHN_EXCL_IND]
				local N`n'_y = e(N)
				mat f = e(first)
				local F_b`n'_y = round(f[8,1], .01)
				cap local F_I_b`n'_y = round(f[8,2], .01)
				
		ivreghdfe `var' ($rhs1 $rhs2 = $ivs1 $ivs2 ) $ctrl , a(nic2 year) first cluster(nic_08_4dig)
				local b`n'_yn = _b[diff_ln_IND]
				local se_b`n'_yn = _se[diff_ln_IND]
				cap local I_b2_yn = _b[diff_wIPT_IND_CHN]
				cap local se_I_b2_yn = _se[diff_wIPT_IND_CHN]
				cap local I_b3_yn = _b[diff_wIPT_LMI_CHN_EXCL_IND]
				cap local se_I_b3_yn = _se[diff_wIPT_LMI_CHN_EXCL_IND]
				local N`n'_yn = e(N)
				mat f = e(first)
				local F_b`n'_yn = round(f[8,1], .01)
				cap local F_I_b`n'_yn = round(f[8,2], .01)		
				
		ivreghdfe `var' ($rhs1 $rhs2 = $ivs1 $ivs2 ) $ctrl , a(nic2#year) first cluster(nic_08_4dig)
				local b`n'_ycn = _b[diff_ln_IND]
				local se_b`n'_ycn = _se[diff_ln_IND]
				cap local I_b2_ycn = _b[diff_wIPT_IND_CHN]
				cap local se_I_b2_ycn = _se[diff_wIPT_IND_CHN]
				cap local I_b3_ycn = _b[diff_wIPT_LMI_CHN_EXCL_IND]
				cap local se_I_b3_ycn = _se[diff_wIPT_LMI_CHN_EXCL_IND]
				local N`n'_ycn = e(N)
				mat f = e(first)
				local F_b`n'_ycn = round(f[8,1], .01)
				cap local F_I_b`n'_ycn = round(f[8,2], .01)

		***
		clear
		set obs 1
		gen bs = `bs'

		gen `var'_b`n'_y = `b`n'_y'
		cap gen `var'_I_b2_y = `I_b2_y'
		cap gen `var'_I_b3_y = `I_b3_y'
		gen `var'_N`n'_y = `N`n'_y'
		gen `var'_F_b`n'_y = `F_b`n'_y'
		cap gen `var'_F_I_b`n'_y = `F_I_b`n'_y'

		gen `var'_b`n'_yn = `b`n'_yn'
		cap gen `var'_I_b2_yn = `I_b2_yn'
		cap gen `var'_I_b3_yn = `I_b3_yn'
		gen `var'_N`n'_yn = `N`n'_yn'
		gen `var'_F_b`n'_yn = `F_b`n'_yn'
		cap gen `var'_F_I_b`n'_yn = `F_I_b`n'_yn'

		gen `var'_b`n'_ycn = `b`n'_ycn'
		cap gen `var'_I_b2_ycn = `I_b2_ycn'
		cap gen `var'_I_b3_ycn = `I_b3_ycn'
		gen `var'_N`n'_ycn = `N`n'_ycn'
		gen `var'_F_b`n'_ycn = `F_b`n'_ycn'
		cap gen `var'_F_I_b`n'_ycn = `F_I_b`n'_ycn'
		save "$tempfold/bs3_welfare_`var'", replace
	
	restore
}

*** Compile results:
use "$tempfold/bs2_welfare_diff_tfpr", clear
foreach var in diff_q_hat_it diff_p_hat_it  {
	merge 1:1 bs using "$tempfold/bs2_welfare_`var'", assert(3) nogen
	}
order bs
save "$tempfold/bs2_welfare", replace

**
use "$tempfold/bs3_welfare_diff_tfpr", clear
foreach var in diff_q_hat_it diff_p_hat_it  {
	merge 1:1 bs using "$tempfold/bs3_welfare_`var'", assert(3) nogen
	}
order bs
save "$tempfold/bs3_welfare", replace


use "$tempfold/bs2_welfare", clear
merge 1:1 bs using "$tempfold/bs3_welfare", nogen
save "$tempfold/cbs", replace

clear
use "$BS_fold/bs_welfare"
append using "$tempfold/cbs"
save "$BS_fold/bs_welfare", replace
